package ui.activity.group_management;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import controller.company.AllCompanyFeedAsync;
import controller.company.DownloadFileAsync;
import controller.group_management.DownloadImageInPreviewPhotoAndVDOTabInGroupFeedFragmentAsync;
import controller.group_management.PhotoAndVDOTabInGroupFeedFragmentAsync;
import define.API;
import define.Key;

/**
 * Created by Dong Le on 5/27/2015.
 */
public class PreviewPhotoAndVDOTabInGroupFeed extends Activity implements View.OnClickListener, View.OnTouchListener{

    //string session
    private FrameLayout mFlInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private ImageButton mIbtnPlayVideoInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private ImageButton mIbtnBackInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private ImageView mIvPhotoInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private ImageView mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private LinearLayout mLlTopInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private LinearLayout mLlBottomInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private TextView mTvDownloadInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private TextView mTvPositionOfFileInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private TextView mTvUserNameInPreviewPhotoAndVDOTabInGroupFeedFragment;
    private VideoView mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment;
    //the others session
    Handler mHandler = new Handler();

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            hideTopAndBottomLlInPreviewPhotoAndVDOTabInGroupFeedFragment();
            mHandler.removeCallbacks(this);
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity:
                finish();
                break;
            case R.id.tv_download_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity:
                // TODO download feature on here
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                    new DownloadFileAsync(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                            PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                                    getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                                    getPreviewMedium());
                else
                    new DownloadFileAsync(this).execute(
                            PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                                    getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                                    getPreviewMedium());
                break;
            case R.id.ibtn_play_video_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity:
                mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
                mIbtnPlayVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
                mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
                String URL = "";
                if (API.IS_DEV_SITE) {
                    URL =  PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                            getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                            getPreviewMedium().replace("https","http");
                } else {
                    URL = PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                            getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                            getPreviewMedium();
                }
                playVideoInPreviewPhotoAndVDOTabInGroupFeedFragment(URL);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_photo_and_vdo_tab_in_group_feed_fragment);

        initialView();;
        initialData();
        setDataForPreviewPhotoAndVDOTabInGroupFeedFragment();
        hideTopAndBottomLlInPreviewPhotoAndVDOTabInGroupFeedFragment();
    }

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        switch (v.getId()) {
            case R.id.fl_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity:
                showTopAndBottomLlInPreviewPhotoAndVDOTabInGroupFeedFragment();
                mHandler.postDelayed(mRunnable, 3000);
                break;
        }
        return false;
    }

    private void initialView () {
        mFlInPreviewPhotoAndVDOTabInGroupFeedFragment = (FrameLayout) findViewById(R.id.fl_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mIbtnPlayVideoInPreviewPhotoAndVDOTabInGroupFeedFragment = (ImageButton) findViewById(R.id.ibtn_play_video_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mIbtnBackInPreviewPhotoAndVDOTabInGroupFeedFragment = (ImageButton) findViewById(R.id.ibtn_back_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mIvPhotoInPreviewPhotoAndVDOTabInGroupFeedFragment = (ImageView) findViewById(R.id.iv_photo_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment = (ImageView) findViewById(R.id.iv_preview_video_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mLlTopInPreviewPhotoAndVDOTabInGroupFeedFragment = (LinearLayout) findViewById(R.id.ll_top_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mLlBottomInPreviewPhotoAndVDOTabInGroupFeedFragment = (LinearLayout) findViewById(R.id.ll_bottom_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mTvDownloadInPreviewPhotoAndVDOTabInGroupFeedFragment = (TextView) findViewById(R.id.tv_download_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mTvPositionOfFileInPreviewPhotoAndVDOTabInGroupFeedFragment = (TextView) findViewById(R.id.tv_position_of_file_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mTvUserNameInPreviewPhotoAndVDOTabInGroupFeedFragment = (TextView) findViewById(R.id.tv_user_name_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment = (VideoView) findViewById(R.id.vv_video_in_preview_photo_and_vdo_tab_in_group_feed_fragment_activity);
    }

    private void initialData () {
        mIbtnBackInPreviewPhotoAndVDOTabInGroupFeedFragment.setOnClickListener(this);
        mTvDownloadInPreviewPhotoAndVDOTabInGroupFeedFragment.setOnClickListener(this);
        mFlInPreviewPhotoAndVDOTabInGroupFeedFragment.setOnTouchListener(this);
        mIbtnPlayVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setOnClickListener(this);
        // set initial for video view
        MediaController mMc = new MediaController(this);
        mMc.setAnchorView(mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment);
        mMc.setMediaPlayer(mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment);
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.requestFocus();
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setBackgroundColor(Color.BLACK);
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setMediaController(mMc);
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setZOrderOnTop(true);
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.isPlaying()) {
                    mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.pause();
                } else {
                    mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.start();
                }

                return false;
            }
        });
    }

    private void setDataForPreviewPhotoAndVDOTabInGroupFeedFragment () {
        if (PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                getContentType() == Key.photo) {
            mIvPhotoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
            mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
            mIbtnPlayVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
            mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new DownloadImageInPreviewPhotoAndVDOTabInGroupFeedFragmentAsync(this, mIvPhotoInPreviewPhotoAndVDOTabInGroupFeedFragment).
                        executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                                PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                                        getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                                        getPreviewMedium());
            else
                new DownloadImageInPreviewPhotoAndVDOTabInGroupFeedFragmentAsync(this, mIvPhotoInPreviewPhotoAndVDOTabInGroupFeedFragment).
                        execute(
                                PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                                        getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                                        getPreviewMedium());
        } else if (PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                getContentType() == Key.video) {
            mIvPhotoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
            mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
            mIbtnPlayVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
            mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new DownloadImageInPreviewPhotoAndVDOTabInGroupFeedFragmentAsync(this, mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment).
                        executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                                PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                                        getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                                        getSplash());
            else
                new DownloadImageInPreviewPhotoAndVDOTabInGroupFeedFragmentAsync(this, mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment).
                        execute(
                                PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                                        getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).
                                        getSplash());
        }
        mTvPositionOfFileInPreviewPhotoAndVDOTabInGroupFeedFragment.setText(String.valueOf(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem() + 1) + " of " +
                String.valueOf(PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).getmAlShotOfPhotoAndVDOTabInGroupFeedItem().size()));
        mTvUserNameInPreviewPhotoAndVDOTabInGroupFeedFragment.setText("By " + PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed.get(Enterprise.groupManagement.getPositionOfPhotoAndVdoTabInGroupFeedItem()).
                getmAlShotOfPhotoAndVDOTabInGroupFeedItem().get(Enterprise.groupManagement.getPositionOfGridViewOfPhotoAndVdoTabInGroupFeedItem()).getOwnerName());
    }

    private void hideTopAndBottomLlInPreviewPhotoAndVDOTabInGroupFeedFragment () {
        mLlBottomInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
        mLlTopInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
    }

    private void showTopAndBottomLlInPreviewPhotoAndVDOTabInGroupFeedFragment () {
        mLlBottomInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
        mLlTopInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
    }

    // play video
    private void playVideoInPreviewPhotoAndVDOTabInGroupFeedFragment(String uri) {
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setBackgroundColor(Color.TRANSPARENT);
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVideoURI(Uri.parse(uri));

        // Begin play video
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.start();
        mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // show iv preview video
                mIvPreviewVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
                // show ibtn play video
                mIbtnPlayVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.VISIBLE);
                // hide video view
                mVvVideoInPreviewPhotoAndVDOTabInGroupFeedFragment.setVisibility(View.GONE);
            }
        });
    }
}
